package com.hzdl.interceptor;

import com.hzdl.bean.User;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * author:Administrator
 * date:2018\9\4 0004
 * description:登陆拦截器：未登录不允许访问后台管理页面
 */
public class LoginInterceptor implements HandlerInterceptor {
    private String[] ignoreList = {"/user/toLogin", "/user/login"};

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {

        String uri = request.getRequestURI();
        HttpSession session = request.getSession();
        if (session.getAttribute("user") == null) {
            boolean flag = false;
            for (String s : ignoreList) {
                if (uri.contains(s)) {
                    return true;
                }

            }
            if (!flag) {
                request.setAttribute("msg", "请先登录再访问!");
                request.getRequestDispatcher("/user/toLogin").forward(request, response);
                return false;
            }
        }
        return true;

    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
